package com.chushouya.manager.dao.repository;

import com.chushouya.manager.dao.entity.StoreEntity;
import com.chushouya.manager.dao.mapper.StoreEntityMapper;
import com.chushouya.manager.dto.admin.store.StoreQuery;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class StoreRepository extends BaseRepository<StoreEntityMapper, StoreEntity, Long> {
    public StoreEntity selectOne(StoreQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    private Example buildExample(StoreQuery query) {
        Example example = this.buildExample();
        if (Objects.nonNull(query.getStoreId())) {
            example.and().andEqualTo("storeId", query.getStoreId());
        }
        if( Strings.isNotEmpty(query.getShopId())) {
            example.and().andEqualTo("shopId", query.getShopId());
        }
        if (Strings.isNotEmpty(query.getStoreName())) {
            example.and().andLike("storeName", Strings.format("%{}%", query.getStoreName()));
        }
        if (Objects.nonNull(query.getStatus())) {
            example.and().andEqualTo("status", query.getStatus());
        }
        if(Strings.isNotEmpty(query.getAlipayOrderId())) {
            example.and().andEqualTo("alipayOrderId", query.getAlipayOrderId());
        }
        if (Strings.isNotEmpty(query.getPid())) {
            example.and().andEqualTo("pid", query.getPid());
        }
        if (Objects.nonNull(query.getProxyId())) {
            example.and().andEqualTo("proxyId", query.getProxyId());
        }
        if (Objects.nonNull(query.getMerchantId())) {
            example.and().andEqualTo("merchantId", query.getMerchantId());
        }
        if (Strings.isNotEmpty(query.getContactPhone())) {
            example.and().andEqualTo("contactPhone", query.getContactPhone());
        }
        example.orderBy("storeId").desc();
        return example;
    }

    public PageResult<StoreEntity> selectPageResult(StoreQuery query) {
        return this.selectPage(query.getPageRequest(), this.buildExample(query));
    }

    public List<StoreEntity> selectList(StoreQuery query) {
        return this.selectByExample(this.buildExample(query));
    }

}
